<!DOCTYPE html>
<html lang='en'>

<head>
  <meta charset='UTF-8'>
  <meta name='viewport' content='width=device-width, initial-scale=1.0'>
  <title>Document</title>
  <style>
    button.backTop {
      position: fixed;
      right: 50px;
      bottom: 100px;
      display: none;
    }
  </style>
</head>

<body style="height: 3000px;">

  <div id='app'>
    <button @click="isShow = !isShow">按钮</button>
    <button class="backTop" v-if="isShow" v-back-top="500">回到顶部</button>
  </div>

  <script src='https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.js'></script>
  <script>
    Vue.directive('back-top', {
      bind(el, binding) {
        window.onscroll = () => {
          let sTop = document.documentElement.scrollTop || document.body.scrollTop;
          // console.log(sTop);
          if (sTop > binding.value) {
            el.style.display = "block";
          } else {
            el.style.display = "none";
          }
        }
        el.onclick = () => {
          window.scrollTo(0, 0)
        }
      }
      ,unbind(){
        window.onscroll = null;
      }
    })

    const app = new Vue({
      el: '#app'
      ,data: {
        isShow: true
      }
    })
  </script>
</body>

</html>